package com.aitemv.web.base;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.aitemv.base.BaseEntity;
import com.aitemv.utils.CommUtil;
/**
 * 
 * @author lyvee
 * 父类action
 */
@Controller
public abstract class BaseAction {
	/*page 用于forward（）返回*/
	public String page = "index";
	@SuppressWarnings("unused")
	private String title="";
	
	/*网站名字*/
	

	/*** 日期转换*/
	@InitBinder
	protected void ininBinder(WebDataBinder binder){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf,true));
	}
	
	/**
	 * 跳转方法（此方法用于单纯跳转，有子acion去实现）
	 * @return
	 */
	public abstract String forward(ModelMap model, HttpServletRequest request, HttpServletResponse response);

	/****************------ getter setter---------------------------- **********/
	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}
	/**
	 * @Title: conditionParam 
	 * @Description: 获取所有requst中的参数和值，然后实例化entity对象并且给entity对象的属性赋值
	 * @param request
	 * @param map<param,entity.property> (个别特殊的参数，譬如前台传的是param=value,然则实际对象中的并没有param属性，则须指定一个与其对应的属性)
	 * @param clazz
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws NoSuchMethodException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException  
	 * @return BaseEntity  返回类型
	 */
	@SuppressWarnings({ "rawtypes", "static-access" })
	public BaseEntity conditionParam(HttpServletRequest request,Map<String, String> map,Class<? extends BaseEntity> clazz) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchFieldException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
		List<String> list = CommUtil.conditFieldByEntity(clazz);
		BaseEntity entity = (BaseEntity) clazz.forName(clazz.getName()).newInstance();
		Method method;
			for(String s :list){
				String value =  request.getParameter(s);
				if(StringUtils.isNotBlank(value)){
					String methodStr = "set"+s.toUpperCase().charAt(0)+s.substring(1,s.length());
					Class clazzField = clazz.getDeclaredField(s).getType();
					method = clazz.getMethod(methodStr, clazzField);
					if(clazzField==int.class){
						method.invoke(entity, Integer.parseInt(value));
					}else{
						method.invoke(entity, value);
					}
				}
			}
			return entity;
	}

	
	
}
